/*
 * @Description: 光盘检测 - 完整性检测
 * @Author: Rfan
 * @Date: 2022-10-14 10:41:01
 * @LastEditTime: 2022-10-21 18:11:15
 */

import { forwardRef, useEffect, useImperativeHandle, useState } from 'react';
import { Button, Checkbox, Space } from 'antd';
import cs from 'classnames';
import classnames from 'classnames/bind';
import { useAddPlanDialogContext } from '../../../contexts/AddPlanDialogContext';
import { CheckProps, EnumIntegralityRuleType } from '../../../../../../dictionaries';
import type { IPropsRule } from '../../../../../../interface';
import styles from '../../../styles/entryPanes.module.scss';
import mainStyles from '../../../styles/main.module.scss';
import { isNullObj } from '@/utils';

const cx = classnames.bind(styles);
const mainCx = classnames.bind(mainStyles);
// EEP真实性规则列表
const CDIntegrity = [4, 5, 6, 7];

// EEP真实性规则列表类型
type TRuleList = IPropsRule<EnumIntegralityRuleType>[];

// EEP真实性完整类型
interface IIntegrityFullRules {
  ruleType: CheckProps.Integrity;
  rules: TRuleList;
}

const CDIntegrityCheck = (props: any, ref: any) => {
  const {
    currentRuleInfo,
    getPropertyRules,
    selectAllRules,
    selectInverseRules,
    getRuleState,
    setRuleState
  } = useAddPlanDialogContext();
  // 规则列表
  const [ruleList, setRuleList] = useState<TRuleList>([]);

  useImperativeHandle(ref, () => ({
    save: () => {
      const fullRules: IIntegrityFullRules = {
        ruleType: CheckProps.Integrity,
        rules: ruleList
      };
      return fullRules;
    }
  }));

  /**
   * @description: 获取到规则后回写
   */
  useEffect(() => {
    if (currentRuleInfo && !isNullObj(currentRuleInfo)) {
      const ruleObj = getPropertyRules(CheckProps.Integrity);
      setRuleList(ruleObj?.rules || []);
    }
  }, [currentRuleInfo]);

  return (
    <div className={cs(mainCx('property-wrap'), cx('property-authenticity'))}>
      <Space className={mainCx('property--title-wrap')} align="center">
        <h6 className={mainCx('property--title')}>光盘格式包</h6>
        <div className={mainCx('select-btn')}>
          <Button
            type="text"
            size="small"
            onClick={() => {
              setRuleList(selectAllRules(CDIntegrity));
            }}
          >
            全选
          </Button>{' '}
          /{' '}
          <Button
            type="text"
            size="small"
            onClick={() => {
              setRuleList(selectInverseRules(CDIntegrity, ruleList));
            }}
          >
            反选
          </Button>
        </div>
      </Space>

      <Space direction="vertical">
        <Checkbox
          checked={getRuleState(EnumIntegralityRuleType.DiskTotalRecordConsistency, ruleList)}
          onChange={(evt) => {
            setRuleState(
              evt.target.checked,
              EnumIntegralityRuleType.DiskTotalRecordConsistency,
              ruleList,
              setRuleList
            );
          }}
        >
          总件数相符性检测：(统计电子文件总件数，并和包内《电子文件归档登记表》中登记的归档电子文件数量比对)
        </Checkbox>
        <Checkbox
          checked={getRuleState(EnumIntegralityRuleType.DiskTotalByteConsistency, ruleList)}
          onChange={(evt) => {
            setRuleState(
              evt.target.checked,
              EnumIntegralityRuleType.DiskTotalByteConsistency,
              ruleList,
              setRuleList
            );
          }}
        >
          总字节数相符性检测：（统计电子文件总字节数，并和包内《电子文件归档登记表》中登记的归档电子文件总字节数比对）
        </Checkbox>
        <Checkbox
          checked={getRuleState(EnumIntegralityRuleType.DiskFilingRange, ruleList)}
          onChange={(evt) => {
            setRuleState(
              evt.target.checked,
              EnumIntegralityRuleType.DiskFilingRange,
              ruleList,
              setRuleList
            );
          }}
        >
          归档范围检测 ：(依据单位归档范围和保管期限表对归档信息包中的元数据和内容数据进行检测)
        </Checkbox>
        <Checkbox
          checked={getRuleState(EnumIntegralityRuleType.DiskContentIntegrality, ruleList)}
          onChange={(evt) => {
            setRuleState(
              evt.target.checked,
              EnumIntegralityRuleType.DiskContentIntegrality,
              ruleList,
              setRuleList
            );
          }}
        >
          信息包内容数据完整性检测：（依据归档信息包元数据中记录的文件数量检测归档信息包中实际包含的电子文件数量。）
        </Checkbox>
      </Space>
    </div>
  );
};

export default forwardRef(CDIntegrityCheck);
